package io.renren.common.utils;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import io.renren.core.entity.LearnQuestionInfoEntity;
import org.apache.commons.compress.utils.Lists;
import org.jsoup.internal.StringUtil;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class LoadSpiderQuestionUtil {
    //    private static final String PNG_URL = "https://www.robopark.com.cn/routine/mpbase/pic/views?fileName=%s.png";
    private static final String IMG_TAG = "<img width=\"100%\" src=\"https://www.robopark.com.cn/routine/mpbase/pic/views?fileName=%s.png\" />";

    public static String buildImgTag(String media) {
        return "<img width=\"100%\" src=\"https://www.robopark.com.cn/routine/mpbase/pic/views?fileName=" + media + ".png\" />";
    }
    public static void main(String[] args) {

    }

    public static List<LearnQuestionInfoEntity> load(String name, Long typeId, Integer real, String chapterName) {
        List<LearnQuestionInfoEntity> list = Lists.newArrayList();
        String str = FileUtil.readString("/Users/abc/个人/mini/" + name, "UTF-8");
        JSONObject jp1 = JSON.parseObject(str);
        JSONArray jp1s = jp1.getJSONObject("data").getJSONArray("question_list");
        int i = 0;
        int n = 1;
        for (Object o : jp1s) {
            i++;
            if (i > 100) {
                n++;
                i = 0;
            }
            JSONObject oo = (JSONObject) o;
            String typeName = oo.getString("type_name");
            String question = "<p>" + oo.getString("question") + "</p>";
            String mediaType = oo.getString("media_type");
            String media = oo.getString("media");
            JSONObject optionMap = oo.getJSONObject("optionMap");
            String answer = oo.getString("answer");

            LearnQuestionInfoEntity learnQuestionInfoEntity = new LearnQuestionInfoEntity();
            learnQuestionInfoEntity.setQuestionContent(question);
            if (!StringUtil.isBlank(media) && Objects.equals(mediaType, "1")) {
                learnQuestionInfoEntity.setQuestionContent(question + "\n" + buildImgTag(media));
            }
            learnQuestionInfoEntity.setQuestionReal(real);
            learnQuestionInfoEntity.setQuestionAnalysis("");

            List<Map<String, Object>> al = Lists.newArrayList();
            Map<String, Object> map = Maps.newTreeMap();
            map.putAll(optionMap);
            for (Map.Entry<String, Object> e : map.entrySet()) {
                Map<String, Object> ai = Maps.newHashMap();
                ai.put("a", e.getKey() + "." + e.getValue());
                ai.put("c", Objects.equals(e.getKey(), answer));
                al.add(ai);
            }
            learnQuestionInfoEntity.setAnswerContent(JSON.toJSONString(al));
            learnQuestionInfoEntity.setChapter(chapterName);
            if (StringUtil.isBlank(chapterName)) {
                learnQuestionInfoEntity.setChapter("练习测试小节" + n);
            }
            learnQuestionInfoEntity.setSort(i);
            learnQuestionInfoEntity.setChapterSort(0);
            learnQuestionInfoEntity.setQuestionTypeId(typeId);

            if ("判断".equals(typeName)) {
                learnQuestionInfoEntity.setQuestionType("judge");
            } else if ("单选".equals(typeName)) {
                learnQuestionInfoEntity.setQuestionType("radio");
            } else {
                continue;
            }
            list.add(learnQuestionInfoEntity);
        }

        return list;
    }
}
